//+------------------------------------------------------------------+
//|                                                        basic.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""

#include <stderror.mqh>
#include <stdlib.mqh>

extern string FixPosition = "FIX POSITIONS";
extern bool exFixPosition = false;
extern double exFixVolume = 0.01;


extern bool exDebug = true;
extern double exTradeRisk = 2;

extern bool exTradeByTime = true;

extern int exTradeTimeStart = 8;
extern int exTradeTimeEnd = 19;


string NameFileSound;



bool b_isTradeTime(){
   if(!exTradeByTime){
      return  (true);
   }


   if(exTradeTimeStart > exTradeTimeEnd){
      if(exTradeTimeStart >= TimeHour(Time[0]) && exTradeTimeEnd <= TimeHour(Time[0])){
         return (true);
      }
   }else{
      if(exTradeTimeStart <= TimeHour(Time[0]) && exTradeTimeEnd >= TimeHour(Time[0])){
         return (true);
      }
   }

   return (false);
}

void b_debug(string str){
   if(exDebug){
      Print(str);
   }
}


double b_setStopLose(double stopLoss, int opTrade){

   int digits = MarketInfo(Symbol(),MODE_DIGITS);
   double pp = 1/MathPow(10, digits);

   double StopLevel = MarketInfo(Symbol(), MODE_STOPLEVEL);   
   //kauf
   if (opTrade == 0){
      if (Bid - stopLoss > StopLevel*pp){  
      }else{
         stopLoss = Bid - (pp*StopLevel);
      }     
   }else{
      if (stopLoss - Ask > StopLevel*pp){
      }else{
         stopLoss = Ask + (pp*StopLevel);
      }
   }       
   stopLoss = NormalizeDouble(stopLoss,digits);
   return (stopLoss);
}

double b_getPriceByPip(int pips){
   int digits = MarketInfo(Symbol(),MODE_DIGITS);
   double pp = 1/MathPow(10, digits-1);

   return (pips * pp);
}


void b_OrderSend(string symbol, int cmd, double volume, double price, int slippage, double stoploss, double takeprofit, string comment="", int magic=0, datetime expiration=0, color arrow_color=CLR_NONE) {

   stoploss = b_setStopLose(stoploss,cmd);
   volume = b_getLotSizeByPrice(price,stoploss);
   
   if(exFixPosition){
      volume = exFixVolume;
   }
   int tN = OrderSend(Symbol(),cmd,volume,price,0,stoploss,takeprofit,comment,5,0,Blue);

                
   if(tN <= 0){
      int lastError = GetLastError();
      Alert("error(",lastError,"): ",ErrorDescription(lastError));
   }
   return (0);

}


void b_OrderModify(int ticket, double price, double stoploss, double takeprofit, datetime expiration, color arrow_color=CLR_NONE) {
   stoploss = b_setStopLose(stoploss,OrderType());
   bool fm = OrderModify(ticket,price,stoploss,takeprofit,expiration,arrow_color); 
   if (fm) {
        //PlaySound(NameFileSound);
   }else{
          int lastError = GetLastError();
          Alert("error(",lastError,"): ",ErrorDescription(lastError));
   }  
}







double b_getLotSize(int pips){

   double lotSize = NormalizeDouble(AccountEquity() *exTradeRisk/100/(pips*(MarketInfo(Symbol(), MODE_LOTSIZE)/10000)),2);
   return (lotSize);
}

double b_getLotSizeByPrice(double prefPrice, double slPrice){
   return (b_getLotSize(MathAbs(prefPrice-slPrice)*MathPow(10,Digits - 1)));
}